<template>
      <div class="addressEdit">

        <div >
          <van-radio-group v-model="radio" style="display: flex;padding:10px;font-size:14px;background: #fff;">
            <van-radio name="0" style="text-align: center;width:50%;">家庭住址</van-radio>
            <van-radio name="1">公司地址</van-radio>
          </van-radio-group>
          <van-address-edit
            :area-list="areaList"
            :address-info="addressInfo"
            show-postal
            show-delete
            :show-set-default="bool"
            show-search-result
            :search-result="searchResult"
            @save="onSave"
            @delete="onDelete"
            @change-detail="onChangeDetail"
          />
        </div>

      </div>
</template>

<script>
  import areaList from './area.json';
  import { mapState } from 'vuex';
  import axios from 'axios';
    export default {
        name: "addressEdit",
      data() {
        return {
          areaList,
          radio:'0',
          bool:false,
          addressInfo:{
            id:0,
            name:'',
            tel:'',
            province:'',
            city:'',
            county:'',
            addressDetail:'',
            areaCode:'',
            postalCode:'',
            isDefault:false
          },
          searchResult: []
        }
      },
      computed: {
        ...mapState('shop',{
          sever:'sever',
        })
      },
      methods: {
          //点击保存
        onSave(con) {
          this.$toast.success('保存成功');
          console.log(con)
          var $this = this;
          this.$http.member.put('vipaddress/'+this.addressInfo.id,{
            username:con.name,
            mobile: con.tel,
            zipcode: con.postalCode,
            province_code: con.areaCode.substring(0,2)+'0000',
            city_code: con.areaCode.substring(0,4)+'00',
            area_code: con.areaCode,
            street_code: "0",
            regions_name: con.province+'-'+con.city+'-'+con.county,
            address: con.addressDetail,
            isdefault: con.isDefault,
            type: $this.radio
          }).then((res)=>{
            // console.log(res)
            if(!res.data.code){

              this.$dialog.alert({
                message: '修改成功!点击返回地址列表'
              }).then(() => {
                this.$router.go(-1)
              });
            }

          }).catch((err)=>{
            console.log(err)
          })
        },
        //删除
        onDelete() {
          // this.$toast('delete');
          this.$http.member.delete('vipaddress/'+this.addressInfo.id)
            .then((res)=>{
              // console.log(res)
              if(!res.data.code){
                this.$toast.success('删除成功！');
                var t = window.setTimeout(()=>{
                  this.$router.go(-1)
                },1000)
              }
            }).catch((err)=>{
              console.log(err)
          })

        },
        //点击详细地址时，默认弹出
        onChangeDetail(val) {
          if (val) {
            // this.searchResult = [{
            //   name: '黄龙万科中心',
            //   address: '杭州市西湖区'
            // }];
          } else {
            this.searchResult = [];
          }
        },
        //返回
        onClickLeft(){
          this.$router.go(-1);
        }
      },
      mounted(){
          // console.log(this.$route.params.item)
        var bool = !this.$route.params.item.isDefault?false:true
        this.addressInfo.id = this.$route.params.item.id;
        this.addressInfo.name = this.$route.params.item.name;
        this.addressInfo.tel = this.$route.params.item.tel;
        this.addressInfo.addressDetail = this.$route.params.item.addressDetails;
        this.addressInfo.areaCode = this.$route.params.item.area_code+'';
        this.addressInfo.postalCode = this.$route.params.item.postalCode;
        this.addressInfo.isDefault = bool;
        // console.log(this.addressInfo)

        //加载提示
        this.$toast.loading({
          mask: true,
          message: '加载中...',
          duration: 1000
        });

      }
    }
</script>

<style scoped>

</style>
